package com.pwt.service;

import com.pwt.entity.MERKMALCONTROL;
import com.pwt.model.what.CharacteristicsModel;
import com.pwt.repository.MERKMALCONTROLRepository;
import com.pwt.repository.WERTEVARRepository;
import com.pwt.utils.MathUtil;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * MERKMALCONTROL service层
 *
 * @author auto generated
 * @date 2022-01-11 09:52:49
 */
@Service
public class MERKMALCONTROLService {

    private MERKMALCONTROLRepository merkmalcontrolRepository;

    private WERTEVARRepository wertevarRepository;

    public MERKMALCONTROLService(MERKMALCONTROLRepository merkmalcontrolRepository, WERTEVARRepository wertevarRepository) {
        this.merkmalcontrolRepository = merkmalcontrolRepository;
        this.wertevarRepository = wertevarRepository;
    }

    public void  updateControlMerkmal(){
       List<CharacteristicsModel>  charList = this.wertevarRepository.querymERKMAL();
       if(charList != null && charList.size() > 0 ){
           for (CharacteristicsModel character:charList){
               List<Double> values = this.wertevarRepository.queryLastValueByK1000AndK2000(character.getK1000(),character.getK2000());
               if(values == null || values.size() < 1) continue;
               List<Double> rXbar = MathUtil.RXbar(values);
               Double avg = MathUtil.avg(values);
               Double Ravg = MathUtil.avg(rXbar);
               MERKMALCONTROL merkmalcontrol = new MERKMALCONTROL();
               merkmalcontrol.setMETEIL(character.getK1000());
               merkmalcontrol.setMEMERKMAL(character.getK2000());
               merkmalcontrol.setMEOGWCONTROL(MathUtil.addDouble(avg,2.66*Ravg));
               merkmalcontrol.setMEUGWCONTROL(MathUtil.subDouble(avg,2.66*Ravg));
               merkmalcontrol.setRMEOGWCONTROL(MathUtil.mulDouble(Ravg,3.267));
               merkmalcontrol.setRMEUGWCONTROL(0.00);
               this.merkmalcontrolRepository.save(merkmalcontrol);
           }
       }
    }
}
